<?php

namespace common\models;

use yii\base\Model;
use yii\data\ActiveDataProvider;

/**
 * ImagesSearch represents the model behind the search form about `common\models\Images`.
 */
class ImagesSearch extends Images
{
    public function attributes()
    {
        return array_merge(parent::attributes(),['typeName']);
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'type', 'create_time', 'Porright', 'collectnum'], 'integer'],
            [['img_name', 'address', 'info','typeName','id'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Images::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'create_time' => SORT_DESC,
                ]
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');

            return $dataProvider;
        }

        $query->andFilterWhere([
//            'id' => $this->id,
            'user_id' => $this->user_id,
            'type' => $this->type,
            'create_time' => $this->create_time,
            'Porright' => $this->Porright,
            'collectnum' => $this->collectnum,
        ]);

        $query->andFilterWhere(['like', 'img_name', $this->img_name])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'info', $this->info])
            ->andFilterWhere(['like','images.id',$this->id]);

        $query->join('INNER JOIN','image_type','images.type = image_type.id');
        $query->andFilterWhere(['like','image_type.type_name',$this->typeName]);
        $dataProvider->sort->attributes['typeName'] =
            [
                'asc'=>['image_type.type_name'=>SORT_ASC],
                'desc'=>['image_type.type_name'=>SORT_DESC],
            ];
//        echo '<pre>'; echo var_dump($dataProvider);exit();
        return $dataProvider;
    }
}
